package com.ruoyi.use.aspect;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.use.domain.ExaminationUse;
import com.ruoyi.use.service.IExaminationUseService;
import com.ruoyi.examination.domain.Examination;
import com.ruoyi.examination.service.IExaminationService;
import com.ruoyi.storeroomuse.service.IStoreroomUseService;
import com.ruoyi.storeroomuse.vo.StoreUseVO;
import com.ruoyi.system.domain.Storeroom;
import com.ruoyi.system.service.IStoreroomService;
import com.ruoyi.system.vo.StoreroomAndUserVO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 入场
@Aspect
@Component
public class ExamEntranceAspect {

    @Autowired
    IExaminationUseService examinationUseService;

    @Autowired
    IStoreroomUseService storeroomUseService;

    @Autowired
    IExaminationService examinationService;

    @Autowired
    IStoreroomService storeroomService;

    @Pointcut("execution(public * com.ruoyi.examination.service.IExaminationService.entrance(..))")
    public void myMethod(){
        //代理方法
    }
    @After("myMethod()")
    public void afterMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object every : args) {
            ExaminationUse examinationUse = new ExaminationUse();
            StoreUseVO storeUseVO = new StoreUseVO();
            if (every instanceof Examination){
                // 添加参试设备列表
                Examination eve=(Examination) every;
                examinationUse.setExaminationId(eve.getExaminationId());
                SysUser sysUser = ShiroUtils.getSysUser();
                examinationUse.setFounder(sysUser.getUserId());
                examinationUse.setOperationName(5L);
                examinationUseService.insertExaminationUse(examinationUse);
                // 添加库房列表
                storeUseVO.setStoreroomId(eve.getStoreroomId());
                storeUseVO.setExaminationId(eve.getExaminationId());
                storeUseVO.setFounder(sysUser.getUserId());
                storeUseVO.setOperationName(3L);
                storeroomUseService.insertStoreroomUse(storeUseVO);
                // 判断库房是否满了
                Long num = examinationService.selectNumByStoreId(eve.getStoreroomId());
                StoreroomAndUserVO storeroomAndUserVO = storeroomService.selectStoreroomByStoreroomId(eve.getStoreroomId());
                Long specification = storeroomAndUserVO.getSpecification();
                if (num.equals(specification)) {
                    Storeroom storeroom = new Storeroom();
                    storeroom.setStoreroomId(storeroomAndUserVO.getStoreroomId());
                    storeroom.setUseStatus(1);
                    storeroomService.updateStoreroom(storeroom);
                }
            }
        }
    }
}
